package com.syaccp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.model.SysDictData;
import com.syaccp.mapper.SysDictDataMapper;
import com.syaccp.request.DictDataDto;
import com.syaccp.service.DictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syaccp.utils.OrderRelatedUtil;
import com.syaccp.utils.RedisUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author lixin
 * @since 2023-07-02
 */
@Service("dictDataService")
public class DictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements DictDataService {

    @Autowired
    SysDictDataMapper sysDictDataMapper;

    @Resource
    private RedisTemplate redisTemplate;


    @Resource
    RedisUtil redisUtil;

    @Override
    public List<SysDictData> queryAllByType() {
        return sysDictDataMapper.queryAllByType();
    }


    @Override
    public List<SysDictData> queryAllByCommentType() {
        return sysDictDataMapper.queryAllByCommentType();
    }

    @Override
    public List<SysDictData> queryAllProduct() {
        return sysDictDataMapper.queryAllProduct();
    }

    @Override
    public List<SysDictData> selectByType(String type) {
        List<SysDictData> list=(List<SysDictData>) redisTemplate.opsForValue().get("dict_"+type);

        if (list==null){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("type",type);
            list=this.baseMapper.selectList(qw);
            //list= dictMapper.selectByType(type);
            redisTemplate.opsForValue().set("dict_"+type,list);
            return list;
        }else {
            return list;
        }

    }




    @Override
    public PageInfo<SysDictData> queryPage(DictDataDto dictDataDto) {
        PageHelper.startPage(dictDataDto.getPageIndex(),dictDataDto.getPageSize());
        QueryWrapper<SysDictData> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(dictDataDto.getType())){
            queryWrapper.eq("type",dictDataDto.getType());
        }
        PageInfo<SysDictData> pageInfo = new PageInfo<>(this.baseMapper.selectList(queryWrapper));
        return pageInfo;
    }

    @Override
    public List<SysDictData> queryStyleInfo() {
        LambdaQueryWrapper<SysDictData> queryWrapper= Wrappers.lambdaQuery(SysDictData.class);
        queryWrapper.eq(SysDictData ::getType,"product_style");
        return sysDictDataMapper.selectList(queryWrapper);
    }



    @Override
    public List<SysDictData> queryOrderQuality() {
        String key = OrderRelatedUtil.SYS_ORDER_QUALITY.getMessage() + "_" + OrderRelatedUtil.ORDER_DROPDOWN;
        if (redisUtil.exists(key)) {
            return redisUtil.get(key);
        }
        LambdaQueryWrapper<SysDictData> qw = Wrappers.lambdaQuery(SysDictData.class);
        qw.select(SysDictData::getId, SysDictData::getLabel, SysDictData::getValue)
                .eq(SysDictData::getType, OrderRelatedUtil.SYS_ORDER_QUALITY.getMessage())
                .orderByAsc(SysDictData::getSort);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(qw);
        redisUtil.set(key, sysDictData, (long) 60 * 60 * 3);
        return sysDictData;
    }

    @Override
    public List<SysDictData> queryOrderAttachmentType() {
        String key = OrderRelatedUtil.SYS_ATTACHMENT_TYPE.getMessage() + "_" + OrderRelatedUtil.ORDER_DROPDOWN;
        if (redisUtil.exists(key)) {
            return redisUtil.get(key);
        }
        LambdaQueryWrapper<SysDictData> qw = Wrappers.lambdaQuery(SysDictData.class);
        qw.select(SysDictData::getId, SysDictData::getLabel, SysDictData::getValue)
                .eq(SysDictData::getType, OrderRelatedUtil.SYS_ATTACHMENT_TYPE.getMessage())
                .orderByAsc(SysDictData::getSort);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(qw);
        redisUtil.set(key, sysDictData, (long) 60 * 60 * 3);
        return sysDictData;
    }

    @Override
    public List<SysDictData> queryOrderStyle() {
        String key = OrderRelatedUtil.SYS_ORDER_FGFL.getMessage() + "_" + OrderRelatedUtil.ORDER_DROPDOWN;
        if (redisUtil.exists(key)) {
            return redisUtil.get(key);
        }
        LambdaQueryWrapper<SysDictData> qw = Wrappers.lambdaQuery(SysDictData.class);
        qw.select(SysDictData::getId, SysDictData::getLabel, SysDictData::getValue)
                .eq(SysDictData::getType, OrderRelatedUtil.SYS_ORDER_FGFL.getMessage())
                .orderByAsc(SysDictData::getSort);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(qw);
        redisUtil.set(key, sysDictData, (long) 60 * 60 * 3);
        return sysDictData;
    }

    @Override
    public List<SysDictData> queryOrderMakeTeam() {
        String key = OrderRelatedUtil.SYS_ORDER_MAKE_TEAM.getMessage() + "_" + OrderRelatedUtil.ORDER_DROPDOWN;
        if (redisUtil.exists(key)) {
            return redisUtil.get(key);
        }
        LambdaQueryWrapper<SysDictData> qw = Wrappers.lambdaQuery(SysDictData.class);
        qw.select(SysDictData::getId, SysDictData::getLabel, SysDictData::getValue)
                .eq(SysDictData::getType, OrderRelatedUtil.SYS_ORDER_MAKE_TEAM.getMessage())
                .orderByAsc(SysDictData::getSort);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(qw);
        redisUtil.set(key, sysDictData, (long) 60 * 60 * 3);
        return sysDictData;
    }
}
